package com.osce.face.controller;

import com.csce.domain.entity.face.FaceList;
import com.csce.domain.entity.face.FaceUserListDTO;
import com.osce.common.utils.http.CompareFaceRecognitionAO;
import com.osce.common.utils.http.CompareFaceRecognitionResDTO;
import com.osce.common.utils.http.CompareFaceRes;
import com.osce.common.utils.http.ImageAO;
import com.csce.domain.entity.face.ao.SlabEnteringFaceAO;
import com.csce.domain.entity.face.ao.SlabEnteringFingerprintAO;
import com.osce.common.annotation.Log;
import com.osce.common.core.controller.BaseController;
import com.osce.common.core.entity.AjaxResult;
import com.osce.common.core.page.TableDataInfo;
import com.osce.common.enums.BusinessType;
import com.osce.common.utils.poi.ExcelUtil;
import com.osce.interfaces.face.IFaceListService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 人脸数据Controller
 *
 * @author zzx
 * @since 2023-11-21
 */
@RestController
@RequestMapping("/face/face_list")
public class FaceListController extends BaseController {

    @Resource
    private IFaceListService faceListService;

    /**
     * 查询人脸数据列表
     */
//    @PreAuthorize("@ss.hasPermi('face:face_list:list')")
//    @GetMapping("/list")
//    public TableDataInfo list(FaceList face) {
//        startPage();
//        List<FaceList> list = faceListService.selectFaceListList(face);
//        return getDataTable(list);
//    }

    /**
     * 查询人脸数据列表
     */
    @PreAuthorize("@ss.hasPermi('face:face_list:list')")
    @GetMapping("/list")
    public TableDataInfo list(FaceUserListDTO face) {
        startPage();
        List<FaceUserListDTO> list = faceListService.selectFaceList(face);
        return getDataTable(list);
    }

    /**
     * 导出人脸数据列表
     */
    @PreAuthorize("@ss.hasPermi('face:face_list:export')")
    @Log(title = "人脸数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FaceList faceList) {
        List<FaceList> list = faceListService.selectFaceListList(faceList);
        ExcelUtil<FaceList> util = new ExcelUtil<>(FaceList.class);
        util.exportExcel(response, list, "人脸数据数据");
    }

    /**
     * 获取人脸数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('face:face_list:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(faceListService.selectFaceListById(id));
    }

    /**
     * 新增人脸数据
     */
    @PreAuthorize("@ss.hasPermi('face:face_list:add')")
    @Log(title = "人脸数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FaceList faceList) {
        return toAjax(faceListService.insertFaceList(faceList));
    }

    @PreAuthorize("@ss.hasPermi('face:face_list:add')")
    @Log(title = "人脸数据", businessType = BusinessType.INSERT)
    @PostMapping("/file/{userId}")
    public AjaxResult add(@RequestParam("file") MultipartFile file, @PathVariable("userId") Long userId) throws IOException {
        return toAjax(faceListService.insertFaceListByFile(file.getBytes(), userId));
    }

    @PreAuthorize("@ss.hasPermi('face:face_list:add')")
    @Log(title = "人脸数据", businessType = BusinessType.INSERT)
    @PostMapping("/base64/{userId}")
    public AjaxResult add(@RequestBody String base64, @PathVariable("userId") Long userId) {
        return toAjax(faceListService.insertFaceListByBase64(base64, userId));
    }

    /**
     * 修改人脸数据
     */
    @PreAuthorize("@ss.hasPermi('face:face_list:edit')")
    @Log(title = "人脸数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FaceList faceList) {
        return toAjax(faceListService.updateFaceList(faceList));
    }

    @PreAuthorize("@ss.hasPermi('face:face_list:edit')")
    @Log(title = "人脸数据", businessType = BusinessType.UPDATE)
    @PutMapping("/file/{userId}")
    public AjaxResult edit(@RequestParam("file") MultipartFile file, @PathVariable("userId") Long userId) throws IOException {
        return toAjax(faceListService.updateFaceListByFile(file.getBytes(), userId));
    }

    @PreAuthorize("@ss.hasPermi('face:face_list:edit')")
    @Log(title = "人脸数据", businessType = BusinessType.UPDATE)
    @PutMapping("/base64/{userId}")
    public AjaxResult edit(@RequestBody String base64, @PathVariable("userId") Long userId) {
        return toAjax(faceListService.updateFaceListByBase64(base64, userId));
    }


    /**
     * 传图片路径修改
     */
    @PreAuthorize("@ss.hasPermi('face:face_list:edit')")
    @Log(title = "人脸数据", businessType = BusinessType.UPDATE)
    @PutMapping("/update/imageUrl")
    public AjaxResult editImage(@RequestBody ImageAO imageAO) {
        return toAjax(faceListService.editImage(imageAO));
    }



    /**
     * 添加传图片路径
     */
    @PreAuthorize("@ss.hasPermi('face:face_list:add')")
    @Log(title = "人脸数据", businessType = BusinessType.INSERT)
    @PutMapping("/add/imageUrl")
    public AjaxResult addImage(@RequestBody ImageAO imageAO) {
        return toAjax(faceListService.addImage(imageAO.getImage(), imageAO.getUserId()));
    }


    /**
     * 删除人脸数据 √
     */
    @PreAuthorize("@ss.hasPermi('face:face_list:remove')")
    @Log(title = "人脸数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(faceListService.deleteFaceListByIds(ids));
    }



    /**
     * 平板新增人脸数据 √
     */
//    @Log(title = "平板录入人脸数据", businessType = BusinessType.INSERT)
    @PreAuthorize("permitAll")
    @PostMapping("/slab/face-add")
    public AjaxResult slabFaceAdd(@RequestBody SlabEnteringFaceAO ao) {
        return toAjax(faceListService.slabFaceAdd(ao));
    }

//    @PreAuthorize("@ss.hasPermi('face:face_list:edit')")
@PutMapping("/compareFaces-recognition")
@PreAuthorize("permitAll")
public AjaxResult callFaceCompareFacesRecognitionService(@RequestBody CompareFaceRecognitionAO ao) {
    CompareFaceRes compareFaceRes = faceListService.callFaceCompareFacesRecognitionService(ao);
    //成功 0
    if (!compareFaceRes.getCode().equals(0)) {
        return error(compareFaceRes.getMsg());
    }

    //成功之后，需要添加签到记录,下面的data是签到的信息用户的id，签到时间
    CompareFaceRecognitionResDTO data = compareFaceRes.getData();

    //考试id
    Integer examinationId = ao.getExaminationId();

    //用户id
    Long userId = data.getUserId();

    //相识度
    Float similarity = data.getSimilarity();

    if (similarity < 0.79) {
        return error("人脸相似度不足");
    }

    return success(compareFaceRes.getData());
}


    /**
     * 平板新增指纹数据
     */
//    @Log(title = "平板录入指纹数据", businessType = BusinessType.INSERT)
    @PreAuthorize("permitAll")
    @PostMapping("/slab/fingerprint-add")
    public AjaxResult slabFingerprintAdd(@RequestBody SlabEnteringFingerprintAO ao) {
        return toAjax(faceListService.slabFingerprintAdd(ao));
    }












}
